<?php


// maker a chche for all page
function SystemAll_cache()
{
  $alls=array();
  $alls= getWebMasterEmailsall();
  cache_write_all("SystemAll","SystemAll",$alls);
}

//category
function category_cache()
{	

	$aryCategories = get_category();	
	array_push($aryCategories,array("id" => 10001,"code"=>"new_item","name"=>"New Product","namezh"=>"新产品","fid"=>0,"seq"=>0,"publish"=>1,"sub_cate"=>array()));
	cache_write("category","_categories",$aryCategories);
}
//ҳͼƬϢд뻺ļ
function indeximg_cache()
{	
	$aryIndexImg = get_indeximg();		
	cache_write("indeximg","_indeximgs",$aryIndexImg);
}




//StyleϢд뻺ļ
function style_cache()
{	
	$aryStyle = get_style();		
	cache_write("style","_style",$aryStyle);
}
//CollectionϢд뻺ļ
function collection_cache()
{	
	$aryCollection = get_collection();		
	cache_write("collection","_collection",$aryCollection);
}
//FinishϢд뻺ļ
function finish_cache()
{		
	//ȡFinish
	//$aryFinsh = get_finish();		
	//cache_write("finish","_finish",$aryFinsh);
	
	global $cachedir, $db;  
	$aryFinsh = array();
	$file_name = JANSEN_ROOT . $cachedir . '/cache_finish.php';
	$strFinishAry = '';
	$aryTmp = array();
	
	$option = '<option value="%s">%s</option>\n';
	$sql = "SELECT id,name FROM `p_finish_set` order by id";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$aryTmp = null;
		$sql2 = "SELECT id,name FROM `p_finish_item` WHERE sid=".$rs["id"]." ORDER BY name";
		$query2 = $db->query($sql2);
		$ops = '';
		while($rs2 = $db->fetch_array($query2)) {
			$ops .= sprintf($option,$rs2["id"],$rs2["name"]);
			$aryTmp[] = "Array('id'=>".$rs2["id"].",'name'=>'".$rs2["name"]."')";
		}
		
		$strFinishAry .= "$"."_finishAry_".$rs["id"]."= Array(".implode(',',$aryTmp).");\r\n";		
		$aryFinsh[$rs["id"]] = $ops;
	}
	
	$cachetext = "<?php\r\n".
		"if(!defined('IN_JANSEN')) exit('Access Denied');\r\n".
		'$'.'_finish='.arrayeval($aryFinsh).";\r\n".
		$strFinishAry."\r\n?>";
	
	writeover($file_name,$cachetext,"rb+",1,1,0);
}
//Finish ItemϢд뻺ļ
function finish_item_cache()
{	
	$aryFinshItem = get_finish_item();		
	cache_write("finish_item","_finish_item",$aryFinshItem);
}

function getWebMasterEmailsall()
{
 
    global $db;
	$sql = "SELECT * From `systemtb` ";
	$query = $db->query($sql);	 
    $systems=array();
  while($rs = $db->fetch_array($query)) 
  {
     $systems[$rs["name"]."zh"]=$rs["valuezh"];
	 $systems[$rs["name"]."en"]=$rs["valueen"];	 
  }	
  
   return  $systems;
}



//get category from my sql
function get_category($fid=0)
{
	global  $db; 	
	$ary = array();
	if($fid == 0) $colOrderBy = 'seq';
	else $colOrderBy = 'name';
	
	$sql = "SELECT `id`, `code`, `name`,`namezh`, `fid`, `seq`, `publish` FROM `category` where fid=".$fid." ORDER BY ".$colOrderBy;
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$sql2 = "SELECT count(id) as num FROM `category` where fid=".$rs["id"];
		$rs2 = $db->get_one($sql2);
		if(count($rs2["num"])>0)
		{
			$rs["sub_cate"]	= get_category($rs["id"]);
		}
		$ary[$rs["id"]] = $rs;
	}	
	return $ary;
}

//ȡҳͼƬ
function get_indeximg()
{
	global  $db; 	
	$ary = array();
	$sql = "SELECT `id`, `name`, `photo`,`imgsharp` FROM `s_indeximg` where ispublish = 1 order by orderindex";
	$aryIndexImg = $db->get_ary($sql);	
	
	return $aryIndexImg;
}


//ȡStyle
function get_style()
{
	global  $db; 	
	$ary = array();
	$sql = "SELECT `id`, `name`,`namezh`,`ispublish` FROM `p_style` where ispublish = 1 order by name";
	$aryIndexImg = $db->get_ary($sql);	
	
	return $aryIndexImg;
}
//ȡCollection
function get_collection()
{
	global  $db; 	
	$ary = array();
	$sql = "SELECT `id`, `name`,`namezh`,`ispublish` FROM `p_collection` where ispublish = 1 order by name";
	$aryIndexImg = $db->get_ary($sql);	
	
	return $aryIndexImg;
}
//ȡFinishѡ
//function get_finish()
//{
//	global  $db;  	
//	//ȡFinish
//	$finishAry = array();
//	$option = '<option value="%s">%s</option>\n';
//	$sql = "SELECT id,name FROM `p_finish_set` order by id";
//	$query = $db->query($sql);
//	while($rs = $db->fetch_array($query)) {
//		$sql2 = "SELECT id,name FROM `p_finish_item` WHERE sid=".$rs["id"]." ORDER BY name";
//		$query2 = $db->query($sql2);
//		$ops = '';
//		while($rs2 = $db->fetch_array($query2)) {
//			$ops .= sprintf($option,$rs2["id"],$rs2["name"]);
//		}
//		$finishAry[$rs["id"]] = $ops;
//	}
//	
//	return $finishAry;
//}

//ȡFinish Item
function get_finish_item()
{
	global  $db; 	
	$finishItemAry = array("0"=>"--");
	//ȡFinish item
	$sql2 = "SELECT id,name FROM `p_finish_item` order by orderindex";
	$query2 = $db->query($sql2);
	while($rs2 = $db->fetch_array($query2)) {
		$finishItemAry[$rs2["id"]] = $rs2["name"];
	}
	
	return $finishItemAry;
}
//лַ
function arrayeval($array, $level = 0) {
	$space = '';
	for($i = 0; $i <= $level; $i++) {
		$space .= "\t";
	}
	$evaluate = "Array\n$space(\n";
	$comma = $space;
	foreach($array as $key => $val) {
		$key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
		$val = !is_array($val) && (!preg_match("/^\-?\d+$/", $val) || strlen($val) > 12 || substr($val, 0, 1)=='0') ? '\''.addcslashes($val, '\'\\').'\'' : $val;
		if(is_array($val)) {
			$evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
		} else {
			$evaluate .= "$comma$key => $val";
		}
		$comma = ",\n$space";
	}
	$evaluate .= "\n$space)";
	return $evaluate;
}

//д뻺
function cache_write($name, $var, $values) {
	
	global $cachedir, $db;
	$cachedir="./zh/cache";
	$file_name = JANSEN_ROOTzh. $cachedir . '/cache_'.$name.'.php';	
	$cachetext = "<?php\r\n".
		"if(!defined('IN_JANSEN')) exit('Access Denied');\r\n".
		'$'.$var.'='.arrayeval($values).
		"\r\n?>";
		
	writeover($file_name,$cachetext,"rb+",1,1,0);
}

function cache_write_all($name,$var,$values)
{
    global $cachedir, $db;
	$cachedir="./zh/cache";	
    $file_name = JANSEN_ROOTzh. $cachedir . '/cache_'.$name.'.php';		
	$cachetext = "<?php\r\n".
		"if(!defined('IN_JANSEN')) exit('Access Denied');\r\n"
		.sliptArray($var,$values).
		"\r\n?>";	
	writeover($file_name,$cachetext,"rb+",1,1,0);
}

function sliptArray($var,$arr)
{
    $tmp_str = "";
	if(isset($arr)&&count($arr)>0)
	{
      foreach ($arr as $k=>$v)
	   {
        $tmp_str .= "\$_".$var."['" . $k . "'] = '" . $v . "';\r\n";		
       }
	}

   return   $tmp_str; 

}
?>